import java.nio.charset.Charset
import java.util.*
import java.util.regex.Matcher
import java.util.regex.Pattern
import kotlin.collections.HashMap

/**
 * Created by Administrator on 2019/05/24.
 */
object D {
    private val sybObj = Any()

    @Throws(Exception::class)
    @JvmStatic
    fun main(args: Array<String>) {
        val aa = "BQBS02345"
        //        StringBuilder sb = new StringBuilder();
        //        for (int i = 0; i < aa.length(); i++)
        //        {
        //            int v = aa.charAt(i) ;
        //            sb.append(Integer.toHexString(v));
        //        }
        //        System.out.println(sb.toString());
        //
        //        StringBuilder sba = new StringBuilder();
        //        byte[] ba = new byte[sb.length()/2];
        //        for (int i = 0; i < sb.length(); i+=2)
        //        {
        //            String a = sb.substring(i, i+2);
        //            sba.append((char)Integer.parseInt(a, 16));
        //        }
        //        System.out.println(sba.toString());
        println(StringUtil.toHexString(aa))
        println("123123s".matches("^\\d+$".toRegex()))
        //        System.out.println(Arrays.toString(pwdToByteArray("12F123D8")));

        val d1 = CloneDemo2()
        d1.str1 = "str1"
        d1.str2 = "str2"
        d1.str3 = "str3"

        val d2 = d1.copy() as CloneDemo2
        println(d2.str3)

        println(Math.log(1024.0))

        val ar = charArrayOf(1.toChar(), 2.toChar(), 3.toChar(), 4.toChar(), 5.toChar())

//        var map = hashMapOf<String, String>()
//        map["as"] = "123"
//        map["av"] = "12345"
//        println(map["as"])

//        val s = 360.toFloat() / 100
//        var start = 0f
//        var j = 0
//        while (start < 360){
//            j++
//            println(start)
//            start += s
//        }
//        print("$j ==> $start")

//        print(UUID.randomUUID().toString())

//        val lenAr = byteArrayOf(0x01.toByte(), 0x01.toByte())
//        val len = ((lenAr[0].toInt() shl 8) and 0xFF00) or (lenAr[1].toInt() and 0x00FF)
//        print("len= $len")


        val str = "我";

        val b = str.toByteArray(Charset.forName("GB2312"));
        b.forEach {
            println(Integer.toHexString(it.toInt() and 0x000000FF))
        }
        val ba = "A".toByteArray(Charset.forName("GB2312"))

        var x = 0x3f and 0x000000FF
        var y = 0x7f and 0x000000FF
        var z = ((x shl 8) or y) and 0x00000FFF
        println("result = ${Integer.toHexString(z)}")
    }
    /**8-16位，数字字母符号组成，不能纯数字 */
    fun judgePwd(pwd: String): Boolean {
        //        String phone = "^[A-Za-z0-9_]\\d{8,}$";
        val reg = "^(?![0-9]+$)[0-9A-Za-z\\w\\W]{8,16}$"
        val pattern = Pattern.compile(reg)
        val matcher = pattern.matcher(pwd)
        return matcher.matches()
    }

    fun dataToByteArray(s: String): ByteArray {
        val car = s.toCharArray()
        val br = ByteArray(car.size)
        for (i in car.indices) {
            br[i] = car[i].toByte()
        }
        return br
    }

    fun pwdToByteArray(pwd: String?): ByteArray? {
        if (null != pwd) {
            val len = pwd.length
            if (len <= 4) {
                return dataToByteArray(pwd)
            } else {
                if (len % 2 == 0) {
                    val byteLen = len / 2
                    if (byteLen <= 4) {
                        val ar = ByteArray(byteLen)
                        val pattern = Pattern.compile("^(([A-F])*([a-f])*(\\d)*)$")
                        val strs = ArrayList<String>()
                        run {
                            var i = 0
                            while (i < pwd.length) {
                                val child = pwd.substring(i, i + 2)
                                if (!pattern.matcher(child).matches()) {
                                    throw RuntimeException("数据应该为16进制")
                                }
                                strs.add(child)
                                i += 2
                            }
                        }
                        for (i in strs.indices) {
                            ar[i] = (Integer.valueOf(strs[i]) and 0xff).toByte()
                        }
                        return ar
                    } else {
                        throw RuntimeException("数据长度非法，最多为4个字节")
                    }
                } else {
                    throw RuntimeException("数据长度非法，应该为偶数")
                }
            }
        } else {
            return null
        }
    }
}
